﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeIn.Core.Modules;
using CodeIn.Ui;
using CodeIn.Modules.Graph.Elements;
using AurelienRibon.Core.Options;
using CodeIn.Core;
using System.Globalization;

namespace CodeIn.Modules.Graph.Modules.Ui.Flow {
	public class Flow : BaseModule {
		protected override void InitializeOverride() {
			Option dampingOpt = new Option(
				"Modules.Graph.Ui.Flow.DampingCoeff", "0.85", false, null, (val, old) => {
					double i;
					if (!double.TryParse(val, NumberStyles.Any, CultureInfo.InvariantCulture, out i))
						return old;
					return val;
				});

			Option repulsionOpt = new Option(
				"Modules.Graph.Ui.Flow.RepulsionCoeff", "1000", false, null, (val, old) => {
					double i;
					if (!double.TryParse(val, NumberStyles.Any, CultureInfo.InvariantCulture, out i))
						return old;
					return val;
				});

			Option attractionOpt = new Option(
				"Modules.Graph.Ui.Flow.AttractionCoeff", "0.03", false, null, (val, old) => {
					double i;
					if (!double.TryParse(val, NumberStyles.Any, CultureInfo.InvariantCulture, out i))
						return old;
					return val;
				});

			OptionManager.Instance.RegisterOption(dampingOpt);
			OptionManager.Instance.RegisterOption(repulsionOpt);
			OptionManager.Instance.RegisterOption(attractionOpt);
		}

		protected override bool CanExecuteOverride(string[] args) {
			if (GraphContext.Instance.CurrentGraph == null)
				return False("Current context graph is not defined.");
			return true;
		}

		protected override void ExecuteOverride(string[] args) {
			FlowView view;
			FlowViewModel vm;

			vm = new FlowViewModel(GraphContext.Instance.CurrentGraph);

			var panel = UiHelper.CreateAndShowPanel("Flow View", PanelSide.MIDDLE, () => {
				view = new FlowView();
				view.Loaded += (s, e) => {
					view.DataContext = vm;
				};
				return view;
			});
		}
	}
}
